class SignupController < ApplicationController
  
  def index
    redirect_to :action => :step1
  end
  
  def step1
    @new_player = Player.new
  end
  
  def verify_email
    player_info = get_info_from_ldap_with_email(params[:new_player][:email])
    
    # Make sure the LDAP Lookup worked.
    if player_info == {}
      puts "No LDAP info, rendering step 1"
      flash[:notice] = "We couldn't find any info for the email address #{params[:new_player][:email]}. Please try again."
      redirect_to :action => :step1
    else
      session[:player_info] = player_info 
      redirect_to :action => :step2
    end
    
  end
  
  def step2
    if session[:image_chosen?]
       session[:image_chosen?] = nil
       @new_player = Player.find(session[:player][:id])
       return 
    end
    
    unless session[:player_info] then redirect_to :action => :step1 end
    
    @new_player = Player.new(session[:player_info])
        
    puts "Errors - " + @new_player.errors.full_messages.join('\n')
    unless @new_player.save!
      flash[:notice] = "There was an error when trying to add that email. Are you sure you don't already have an account?"
      redirect_to :action => :step1
    else
      session[:player] = @new_player
    end
  end
  
  def step3
    @new_player = Player.find(params[:new_player][:id])
    @new_player.generate_activation_hash
  end
  
  def upload_image
    @new_player = Player.find(params[:new_player][:id])
    @new_player.uploaded_data = params[:new_player][:uploaded_data]
    
    unless @new_player.save!
      puts "Couldn't save image!"
      redirect_to :back
    end
    
    session[:crop_referrer] = request.env["HTTP_REFERER"]
    redirect_to :controller => :site_main, :action => :crop, :id => @new_player
  end
  
  private
  
  def get_info_from_ldap_with_email(email)
    user_id = email.split("@").first
    
    # Do LDAP Stuff here.
    
    player_info = {}
    
    if user_id == "test236"
      player_info[:first_name] = "Patrick"
      player_info[:last_name] = "Gibson"
      player_info[:faculty] = "School of Computer Science"
      player_info[:email] = "gibsont@uwindsor.ca"
    end
    return player_info
  end
  
end
